package com.mlethe.library.multi.api;

import android.content.Context;
import android.text.TextUtils;

import com.mlethe.library.multi.api.listener.OnActionCallback;
import com.sina.weibo.sdk.common.UiError;

import java.util.HashMap;
import java.util.Map;

/**
 * 微博分享回调处理类
 *
 * @Author Mlethe
 * @Date 2025/3/28
 */
final class WbShareCallback implements com.sina.weibo.sdk.share.WbShareCallback {

    private OnActionCallback mOnActionCallback;

    public WbShareCallback(OnActionCallback callback) {
        this.mOnActionCallback = callback;
    }

    @Override
    public void onComplete() {
        MultiApi.getInstance().i("WEIBO handler onComplete.");
        MultiMedia media = MultiMedia.WEIBO;
        OnActionCallback callback = mOnActionCallback;
        mOnActionCallback = null;
        Map<String, String> map = new HashMap<>();
        if (callback != null) {
            callback.onComplete(media, map);
        }
    }

    @Override
    public void onError(UiError uiError) {
        int errorCode = -9000;
        String errorMsg = "";
        if (uiError != null) {
            errorCode = uiError.errorCode;
            errorMsg = uiError.errorMessage;
        }
        if (TextUtils.isEmpty(errorMsg)) {
            Context context = MultiApi.getInstance().getContext();
            errorMsg = context.getString(R.string.multi_api_privilege_grant_failed);
        }
        MultiApi.getInstance().e("WEIBO handler fail. code=" + errorCode + ", message=" + errorMsg);
        MultiMedia media = MultiMedia.WEIBO;
        OnActionCallback callback = mOnActionCallback;
        mOnActionCallback = null;
        if (callback != null) {
            callback.onFailure(media, errorCode, errorMsg);
        }
    }

    @Override
    public void onCancel() {
        MultiApi.getInstance().i("WEIBO handler cancel.");
        MultiMedia media = MultiMedia.WEIBO;
        OnActionCallback callback = mOnActionCallback;
        mOnActionCallback = null;
        if (callback != null) {
            callback.onCancel(media);
        }
    }
}
